var app = getApp();

import WeCropper from '../resource/we-cropper/we-cropper.js'

const device = wx.getSystemInfoSync();
const width = device.windowWidth;
const height = device.windowHeight-50;

Page(Object.assign({},{
    data:{
        inited : false,
        cropperOpt1: {
            id: 'cropper',
            width : width,
            height : height,
            scale: 2,
            zoom: 8,
            cut: {
                x: (width - 300) / 2,
                y: (height - 300) / 2,
                width: 300,
                height: 300
            }
        },
        cropperOpt2: {
            id: 'cropper',
            width : width,
            height : height,
            scale: 2,
            zoom: 8,
            cut: {
                x: (width - 375) / 2,
                y: (height - 150) / 2,
                width: 375,
                height: 150
            }
        },
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;
        
        app.com.auth(function(user){
            app.com.http('admin','POST',{op:'set'},0,false,'','',function(res){
                if (!res.data.errno) {
                    self.setData({
                        shop : res.data.data.shop,
                        sets : res.data.data.set,
                        params : res.data.data.params,
                    },function(){
                        self.setData({
                            inited : true,
                        });
                    });
                    // 关闭下拉刷新
                    wx.stopPullDownRefresh();
                }else{
                    app.com.alert( res.data.message,function(){
                        app.globalData.adminurl = app.com.getUrlArgs();
                        if( res.data.errno == 2 ){
                            wx.navigateTo({
                                url : res.data.data.url
                            });
                        }
                    });
                }
            });
        },self);

        this.crop1 = new WeCropper(self.data.cropperOpt1);
        this.crop2 = new WeCropper(self.data.cropperOpt2);

        wx.setNavigationBarTitle({
          title: '设置',
        });
        
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: '设置',
            path: '',
        }
    },
    getAddress(e){
        var self = this;
        wx.chooseLocation({
            success : function(res){
                console.log(res)
                self.setData({
                    'shop.lat' : res.latitude,
                    'shop.lng' : res.longitude,
                    'shop.province' : '',
                    'shop.city' : '',
                    'shop.country' : '',
                    'shop.street' : res.address,
                });
            }
        });
    },
    inputvalue(e){
        var self = this;
        var type = e.currentTarget.dataset.type;
        var value = e.detail.value;
        if( type == 'severtime' ){
            var id = e.currentTarget.dataset.id;
            var key =  'shop.severtime.'+id;
            self.setData({
                [key] : value
            })
        }else{
            self.setData({
                ['shop.'+type] : value,
            })
        }
    },
    uploadTap(e){
        var self = this;
        var type = e.currentTarget.dataset.type;
        self.setData({
            dealtype : type
        });
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success (res) {
                var  src = res.tempFilePaths[0];
                if( type == 'head' ) {
                    self.crop1.pushOrign(src);
                    self.setData({
                        showcanvas1 : true,
                    })
                }else if( type == 'bg' ){
                    self.crop2.pushOrign(src);
                    self.setData({
                        showcanvas2 : true,
                    })
                }
                app.com.alert( '请拖动到合适区域' );
            }
        })
    },
    getCropperImage(){
        var self = this;
        if( self.data.dealtype == 'head' ){

            self.crop1.getCropperImage((src) => {
                if (src) {
                    app.com.uptomaster(src,function(res){
                        self.setData({
                            'shop.headimg' : res.attachment,
                            'shop.showheadimg' : res.url,
                            'shop.headtemp' : '',
                        });
                    });
                } else {
                    app.com.alert( '裁剪失败' );
                }
                self.setData({
                    showcanvas1 : false,
                })

            })

        }else if( self.data.dealtype == 'bg' ){
            self.crop2.getCropperImage((src) => {
                if (src) {
                    app.com.uptomaster(src,function(res){
                        self.setData({
                            'shop.cover' : res.attachment,
                            'shop.showcover' : res.url,
                            'shop.covertemp' : '',
                        });
                    });
                } else {
                    app.com.alert( '裁剪失败' );
                }
                self.setData({
                    showcanvas2 : false,
                })

            })
        }
    },
    save(){
        var self = this;
        var shop = self.data.shop;

        if( !shop.name ) {
            app.com.toast('请填入名称');return false;
        }
        if( !shop.desc ) {
            app.com.toast('请填入描述');return false;
        }      
        if( !shop.tel ) {
            app.com.toast('请填入电话');return false;
        }
        if( !shop.headimg ) {
            app.com.toast('请上传头像');return false;
        }
        if( !shop.cover ) {
            app.com.toast('请上传背景图');return false;
        }        
        self.savedata();
    },
    savedata(){
        var self = this;
        var form = JSON.stringify( self.data.shop );
        app.com.http('admin','POST',{op:'saveset',form:form},0,true,'','',function(res){
            if (!res.data.errno) {
                app.com.toast(res.data.message);
            }else{
                app.com.toast( res.data.message );
                if( res.data.errno == 2 ){
                    wx.navigateTo({
                        url : res.data.data.url
                    });
                }
            }
        });
    },
    radio(e){
        var self = this;
        var type = e.currentTarget.dataset.type;
        var value = e.currentTarget.dataset.v;
        var key =  'shop.'+type;
        self.setData({
            [key] : value
        })
    },
    touchStart (e) {
        if( this.data.dealtype == 'head' ){
            this.crop1.touchStart(e)
        }else if( this.data.dealtype == 'bg' ){
            this.crop2.touchStart(e)
        }
    },
    touchMove (e) {
        if( this.data.dealtype == 'head' ){
            this.crop1.touchMove(e)
        }else if( this.data.dealtype == 'bg' ){
            this.crop2.touchMove(e)
        }
    },
    touchEnd (e) {
        if( this.data.dealtype == 'head' ){
            this.crop1.touchEnd(e)
        }else if( this.data.dealtype == 'bg' ){
            this.crop2.touchEnd(e)
        }
    },
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    
}))